﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

namespace deal_h2ck
{
    public class SqlDataProvider
    {
        private static string error;
        public static string Error { get { return error; } private set { error = value; } }
        public static SqlConnection DataConnection()
        {
            string cnStr = ConfigurationManager.ConnectionStrings["strDeal_h2kc"].ConnectionString;
            SqlConnection cn = new SqlConnection(cnStr);
            return cn;
        }
        public static DataTable LoadSQL(string sql)
        {
            try
            {
                SqlConnection cn = DataConnection();
                SqlDataAdapter da = new SqlDataAdapter(sql, cn);
                DataTable dt = new DataTable();
                da.Fill(dt);
                return dt;
            }
            catch(Exception ex)
            {
                Error = ex.Message;
                return null;
            }
            
        }
        public static bool ExecuteQuery(string sql)
        {
            try
            {
                SqlConnection cn = DataConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                return true;
            }
            catch(Exception ex) 
            {
                Error = ex.Message;
                return false;
            }
        }
        public static object ExecuteScalar(string sql)
        {
            try
            {
                object value;
                SqlConnection cn = DataConnection();
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Connection.Open();
                value = cmd.ExecuteScalar();
                cmd.Connection.Close();
                return value;
            }
            catch(Exception ex)
            {
                Error = ex.Message;
                return null;
            }
        }
    }
}